package com.example.customeview;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

/**
 * @author Caleb_F
 * @description:
 * @date :2020-09-24 14:18
 */
public class RecyclerVIewDemo extends AppCompatActivity{
    private RecyclerView myrecycler;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        myrecycler = (RecyclerView) findViewById(R.id.myrecycler);
        //item之间加上间隔
        myrecycler.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.left = 4;
                outRect.top = 4;
            }
        });
  /*      myrecycler.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,
                false));*/

  myrecycler.setLayoutManager(new GridLayoutManager(this,3));
        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            strings.add(new StringBuilder("第").append(i).append("item").toString());
        }
        myrecycler.setAdapter(new MyAdapter(this, strings));
    }
}
